package com.aiiju.core.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class UrlUtils {
	
	/**
	* 检测网络资源是否存在　
	* 判断一个url 路径对应的资源是否存在
	* (准确的说，这个链接是否是有效的链接，能否请求到资源)
	* @param strUrl
	* @return
	*/
	public static boolean isNetFileAvailable(String strUrl) {
		InputStream netFileInputStream = null;
		try {
			URL url = new URL(strUrl);
			URLConnection urlConn = url.openConnection();
			netFileInputStream = urlConn.getInputStream();
			if (null != netFileInputStream) {
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			return false;
		} finally {
			try {
				if (netFileInputStream != null)
					netFileInputStream.close();
			} catch (IOException e) {
			}
		}
	}
	
	/**
	 * 把url 转化成输入流对象
	 * @Description: 转换成流对象以后，就可以获取数据
	 * @author:tianming
	 * @param url
	 * @throws Exception
	 * @time:2017年9月1日 下午1:25:07
	 */
	public static InputStream urlToIO(String urlStr) throws Exception {
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5 * 1000);
		InputStream input = conn.getInputStream();// 通过输入流获取图片数据
		return input;
	}

}
